package com.warning_speed_android.socket;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class ClientHandler extends IoHandlerAdapter {
	public interface Callback {
		void connected();

		void loggedIn();

		void loggedOut();

		void disconnected();

		void messageReceived(String message);

		void messageSend(String message);

		void error(String message);
	}

	private final Callback callback;

	public ClientHandler(Callback callback) {
		this.callback = callback;
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		callback.connected();
	}

	@Override
	public void messageReceived(IoSession session, Object messageObject)
			throws Exception {
		String theMessage = (String) messageObject;
		callback.messageReceived(theMessage);
		// JsonObject message = gson.fromJson(theMessage, JsonObject.class);
		// System.out.println(""+messageObject);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
		// callback.messageSend("SEND: " + message.toString() + "\n");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		callback.disconnected();
	}
}
